/*
 * MainGUI.java
 *
 * Created on Dec 7, 2010, 11:20:45 AM
 */
package optimizerjs;

import java.io.File;
import javax.swing.JFileChooser;
import javax.swing.JTextArea;
import javax.swing.JTextField;

/**
 *
 * @author tantrieuf31@gmail.com (Trieu Nguyen)
 */
public class MainGUI extends javax.swing.JPanel {

    final JFileChooser fc = new JFileChooser();
    protected CompressJS compressJS;

    public CompressJS getCompressJS() {
        if (compressJS == null) {
            compressJS = new CompressJS();
        }
        return compressJS;
    }

    public void setCompressJS(CompressJS compressJS) {
        this.compressJS = compressJS;
        this.compressJS.setTxtareaLog(txtareaLog);
        btnOk.setEnabled(!this.compressJS.isAutoCompile());
    }

    public JTextField getTxtIndexFilePath() {
        return txtIndexFilePath;
    }

    public void setTxtIndexFilePath(JTextField txtIndexFilePath) {
        this.txtIndexFilePath = txtIndexFilePath;
    }

    public JTextField getTxtOptimizedFilePath() {
        return txtOptimizedFilePath;
    }

    public void setTxtOptimizedFilePath(JTextField txtOptimizedFilePath) {
        this.txtOptimizedFilePath = txtOptimizedFilePath;
    }

    public JTextArea getTxtareaLog() {
        return txtareaLog;
    }

    public void setTxtareaLog(JTextArea txtareaLog) {
        this.txtareaLog = txtareaLog;
    }

    /** Creates new form MainGUI */
    public MainGUI() {
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        btnCancel = new javax.swing.JButton();
        btnOk = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        txtareaLog = new javax.swing.JTextArea();
        txtIndexFilePath = new javax.swing.JTextField();
        txtOptimizedFilePath = new javax.swing.JTextField();
        btnIndexFileFinder = new javax.swing.JButton();
        btnOptimizedFileFinder = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();

        setPreferredSize(new java.awt.Dimension(680, 420));

        btnCancel.setLabel("Cancel and Skip");
        btnCancel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCancelActionPerformed(evt);
            }
        });

        btnOk.setLabel("Check and Optimize");
        btnOk.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnOkActionPerformed(evt);
            }
        });

        txtareaLog.setColumns(20);
        txtareaLog.setRows(5);
        jScrollPane1.setViewportView(txtareaLog);

        btnIndexFileFinder.setText("Path Finder");
        btnIndexFileFinder.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnIndexFileFinderActionPerformed(evt);
            }
        });

        btnOptimizedFileFinder.setText("Path Finder");
        btnOptimizedFileFinder.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnOptimizedFileFinderActionPerformed(evt);
            }
        });

        jLabel1.setText("The index.html path");

        jLabel2.setText("The optimized-all.js path");

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(jScrollPane1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 653, Short.MAX_VALUE)
                    .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
                        .add(layout.createSequentialGroup()
                            .add(btnCancel)
                            .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                            .add(btnOk))
                        .add(org.jdesktop.layout.GroupLayout.LEADING, layout.createSequentialGroup()
                            .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                                .add(jLabel1)
                                .add(jLabel2))
                            .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                                .add(layout.createSequentialGroup()
                                    .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                                    .add(txtOptimizedFilePath, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 358, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                                .add(layout.createSequentialGroup()
                                    .add(9, 9, 9)
                                    .add(txtIndexFilePath, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 357, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)))
                            .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                            .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                                .add(btnOptimizedFileFinder)
                                .add(btnIndexFileFinder)))))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(layout.createSequentialGroup()
                        .addContainerGap(21, Short.MAX_VALUE)
                        .add(btnIndexFileFinder)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED))
                    .add(layout.createSequentialGroup()
                        .addContainerGap()
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                            .add(jLabel1)
                            .add(txtIndexFilePath, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)))
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(btnOptimizedFileFinder)
                    .add(jLabel2)
                    .add(txtOptimizedFilePath, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jScrollPane1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 251, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .add(18, 18, 18)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(btnOk)
                    .add(btnCancel))
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents

    private void btnOkActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnOkActionPerformed
        getCompressJS().startCompile();
    }//GEN-LAST:event_btnOkActionPerformed

    private void btnCancelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCancelActionPerformed
        System.exit(0);
    }//GEN-LAST:event_btnCancelActionPerformed

    private void btnIndexFileFinderActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnIndexFileFinderActionPerformed
        int returnVal = fc.showOpenDialog(this);
        if (returnVal == JFileChooser.APPROVE_OPTION) {
            File file = fc.getSelectedFile();
            txtIndexFilePath.setText(file.getAbsolutePath());
            System.out.println("Opening: " + file.getAbsolutePath() + ".");
        } else {
            System.out.println("Open command cancelled by user.");
        }
    }//GEN-LAST:event_btnIndexFileFinderActionPerformed

    private void btnOptimizedFileFinderActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnOptimizedFileFinderActionPerformed
        int returnVal = fc.showOpenDialog(this);
        if (returnVal == JFileChooser.APPROVE_OPTION) {
            File file = fc.getSelectedFile();
            txtOptimizedFilePath.setText(file.getAbsolutePath());
            System.out.println("Opening: " + file.getAbsolutePath() + ".");
        } else {
            System.out.println("Open command cancelled by user.");
        }
    }//GEN-LAST:event_btnOptimizedFileFinderActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnCancel;
    private javax.swing.JButton btnIndexFileFinder;
    private javax.swing.JButton btnOk;
    private javax.swing.JButton btnOptimizedFileFinder;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTextField txtIndexFilePath;
    private javax.swing.JTextField txtOptimizedFilePath;
    private javax.swing.JTextArea txtareaLog;
    // End of variables declaration//GEN-END:variables
}
